//
//  HomeViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//DiwanMishafi
//LcdD
#import "HomeViewController.h"
#import "WeatherViewCell.h"
#import "QMCycelScrollViewCell.h"
#import "QMDoctorViewCell.h"
#import "QMQCCityViewController.h"
#import "QMQCCitySecondViewController.h"
#import "NetWorkManager.h"
#import "QcWeatherModel.h"
#import "QMQCIllnessViewController.h"
#import "QMQCPublicViewController.h"
#import "QMQCMApViewController.h"
@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, copy) NSString *cityName;
@property (nonatomic, strong) QcWeatherModel *cityModel;
@property (nonatomic, copy) NSString *illName;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    NSString *cityName = @"南京";
    [self loadDataWithCityName:cityName];
    [self setupUI];
  
}

#pragma mark
#pragma mark
#pragma mark loadData

- (void)loadDataWithCityName:(NSString *)cityName
{
    
    [[NetWorkManager sharedManager] loadWeatherWithCurrentCity:cityName successBlock:^(NSDictionary *cityDic) {
   
        QcWeatherModel *model = [QcWeatherModel new];
        
        [model setValuesForKeysWithDictionary:cityDic];
        
        
        self.cityModel = model;
        
    } errorBlock:^(NSString *error) {
        
        NSLog(@"%@",error);
    }];
}


#pragma mark
#pragma mark
#pragma mark setupUI
- (void)setupUI {
    
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    
    [addBtn setImage:[UIImage imageNamed:@"map"] forState:UIControlStateNormal];
    
    [addBtn addTarget:self action:@selector(useMap) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
    
    
    self.navigationItem.rightBarButtonItem =  rightItem;
    
    self.cityName = @"南京";
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = kThemeColor;

    self.title = @"祖传老中医 专治吹牛逼";

    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    
    
      titleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    titleLabel.text =  @"祖传老中医 专治吹牛逼";
    
    titleLabel.font = [UIFont fontWithName:@"CloudShuiZhuGBK" size:18];
    self.navigationItem.titleView = titleLabel;
    

    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    
    tableView.estimatedRowHeight = 80;
    
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WeatherViewCell" bundle:nil] forCellReuseIdentifier:@"weather"];
    [self.tableView registerNib:[UINib nibWithNibName:@"QMDoctorViewCell" bundle:nil] forCellReuseIdentifier:@"doctor"];
    
    
    
    [self.tableView registerClass:[QMCycelScrollViewCell class] forCellReuseIdentifier:@"cell"];
    
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}
- (void)useMap
{
    QMQCMApViewController *vc = [[QMQCMApViewController alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark
#pragma mark
#pragma mark tableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
        self.tableView.rowHeight = 120;
        
        WeatherViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weather" forIndexPath:indexPath];
        
                
        [cell.btn_address addTarget:self action:@selector(chooseCity) forControlEvents:UIControlEventTouchUpInside];
        
        if (self.cityName) {
            
            [cell.btn_address setTitle:self.cityName forState:UIControlStateNormal];
            cell.btn_address.titleLabel.font = [UIFont fontWithName:@"CloudShuiZhuGBK" size:20];
            
            if (self.cityModel) {
                
                cell.model = self.cityModel;
                
            }
        }
        return cell;
    }else if (indexPath.row == 1){
        
        QMCycelScrollViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
        tableView.rowHeight = 180;
        return cell;
    }
    
    QMDoctorViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"doctor" forIndexPath:indexPath];
    
    [cell.btn_zl addTarget:self action:@selector(didClickZLBtn) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_xxg addTarget:self action:@selector(didClickXXGBtn) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_xyk addTarget:self action:@selector(didClickXYKBtn) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_sjk addTarget:self action:@selector(didClickSJKBtn) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_gk addTarget:self action:@selector(didClickGKBtn) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_pub addTarget:self action:@selector(didClickPubBtn) forControlEvents:UIControlEventTouchUpInside];
    
    tableView.rowHeight = 370;
    return cell;
}

#pragma mark
#pragma mark
#pragma mark doctorcell 点击方法

- (void)didClickPubBtn
{
    QMQCPublicViewController *pub = [[QMQCPublicViewController alloc] init];
    
    pub.title = @"公益";
    
    [self.navigationController pushViewController:pub animated:YES];
}

//肿瘤
- (void)didClickZLBtn
{
    
    QMQCIllnessViewController *vc = [[QMQCIllnessViewController alloc] init];
    
    vc.title = @"肿瘤";
    vc.name = @"肿瘤疾病";
    vc.cellID = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}
//心血管
- (void)didClickXXGBtn
{
    
    QMQCIllnessViewController *vc = [[QMQCIllnessViewController alloc] init];
    vc.title = @"心血管";
    
    vc.name = @"心血管疾病";
    vc.cellID = @"2";
    [self.navigationController pushViewController:vc animated:YES];
}
//神经科
- (void)didClickSJKBtn
{
    
    QMQCIllnessViewController *vc = [[QMQCIllnessViewController alloc] init];
    vc.title = @"神经科";
    
    vc.name = @"神经疾病";
    vc.cellID = @"3";
    [self.navigationController pushViewController:vc animated:YES];
}
//血液科
- (void)didClickXYKBtn
{
    
    QMQCIllnessViewController *vc = [[QMQCIllnessViewController alloc] init];
    vc.title = @"血液科";
    
    vc.name = @"血液疾病";
    vc.cellID = @"4";
    [self.navigationController pushViewController:vc animated:YES];
}
//骨科
- (void)didClickGKBtn
{
    
    QMQCIllnessViewController *vc = [[QMQCIllnessViewController alloc] init];
    vc.title = @"骨科";
    
    vc.name = @"骨科疾病";
    vc.cellID = @"5";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark
#pragma mark
#pragma mark 视图将要出现的时候 接收通知
- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveCityName:) name:@"CITYNAME" object:nil];

}

#pragma mark
#pragma mark
#pragma mark 通知接收城市名称方法
- (void)receiveCityName:(NSNotification *)noti
{
    
    self.cityName = noti.object;
    
    [self loadDataWithCityName:self.cityName];
    
    
    [self.tableView reloadData];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [self loadDataWithCityName:self.cityName];
    
    [self.tableView reloadData];
}

#pragma mark
#pragma mark
#pragma mark 城市按钮的点击事件 跳转控制器
- (void)chooseCity
{
    QMQCCityViewController *vc = [[QMQCCityViewController alloc] init];
    
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
